package net.lahuobao.mall.controller.admin;
import net.lahuobao.mall.entity.admin.MallEtcCardRuleEntity;
import net.lahuobao.mall.entity.admin.MallEtcSupplierEntity;
import net.lahuobao.mall.service.admin.MallEtcCardRuleServiceI;
import net.lahuobao.mall.service.admin.MallEtcSupplierServiceI;
import org.apache.log4j.Logger;
import org.hibernate.exception.ConstraintViolationException;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Validator;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @Title: Controller
 * @Description: 商城ETC卡卡号管理
 * @author zhangdaihao
 * @date 2016-12-02 14:50:58
 * @version V1.0
 *
 */
@Controller
@RequestMapping("/mallEtcCardRuleController")
public class MallEtcCardRuleController extends BaseController {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(MallEtcCardRuleController.class);

    @Autowired
    private MallEtcCardRuleServiceI mallEtcCardRuleService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private Validator validator;

    @Autowired
    private MallEtcSupplierServiceI etcSupplierService;

    /**
     * 商城ETC卡卡号管理列表 页面跳转
     *
     * @return
     */
    @RequestMapping(params = "list")
    public ModelAndView list(HttpServletRequest request) {
        return new ModelAndView("net/lahuobao/mall/admin/mallEtcCardRuleList");
    }

    /**
     * easyui AJAX请求数据
     *
     * @param request
     * @param response
     * @param dataGrid
     * @param
     */

    @RequestMapping(params = "datagrid")
    public void datagrid(MallEtcCardRuleEntity mallEtcCardRule,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(MallEtcCardRuleEntity.class, dataGrid);

        cq.createAlias("shop","MallShopEntity");
		/*店铺名称*/
        if(null != mallEtcCardRule.getShop() && StringUtil.isNotEmpty(mallEtcCardRule.getShop().getShopname())){
            cq.like("MallShopEntity.shopname", "%"+mallEtcCardRule.getShop().getShopname()+"%");
            mallEtcCardRule.getShop().setShopname(null);
        }

        //卡号
        if(StringUtil.isNotEmpty(mallEtcCardRule.getCardno())){
            cq.like("cardno","%"+ mallEtcCardRule.getCardno()+"%");
            mallEtcCardRule.setCardno(null);
        }

        //车牌号
        if(null!=mallEtcCardRule.getEtcUser() && StringUtil.isNotEmpty( mallEtcCardRule.getEtcUser().getCarno())){
            cq.createAlias("etcUser","MallEtcUserEntity");
            cq.like("MallEtcUserEntity.carno","%"+ mallEtcCardRule.getEtcUser().getCarno()+"%");
            mallEtcCardRule.getEtcUser().setCarno(null);
        }

        //状态
        if(StringUtil.isNotEmpty(mallEtcCardRule.getState())){
            cq.eq("state", mallEtcCardRule.getState());
            mallEtcCardRule.setState(null);
        }

        //排序
        cq.addOrder("createtime", SortDirection.desc);

        //查询条件组装器
        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mallEtcCardRule, request.getParameterMap());
        this.mallEtcCardRuleService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 删除商城ETC卡卡号管理
     *
     * @return
     */
    @RequestMapping(params = "del")
    @ResponseBody
    public AjaxJson del(MallEtcCardRuleEntity mallEtcCardRule, HttpServletRequest request) {
        String message = null;
        AjaxJson j = new AjaxJson();
        mallEtcCardRule = systemService.getEntity(MallEtcCardRuleEntity.class, mallEtcCardRule.getId());
        if(mallEtcCardRule.getState() == 2){
            message = "删除失败！该卡正在使用，不能删除！";
            j.setSuccess(false);
        }else{
            message = "删除成功";
            mallEtcCardRuleService.delete(mallEtcCardRule);
            systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
        }
        j.setMsg(message);
        return j;
    }


    /**
     * 获取添加页面
     *
     * @return
     */
    @RequestMapping(params = "saveview")
    public ModelAndView saveview(HttpServletRequest request) {
        return new ModelAndView("net/lahuobao/mall/admin/mallEtcCardruleAdd");
    }


    /**
     * 获取添加选择商品页面
     *
     * @return
     */
    @RequestMapping(params = "productview")
    public ModelAndView productview(HttpServletRequest request, String shopname, Integer shopid) {
        try {
            shopname = URLDecoder.decode(shopname, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        request.setAttribute("shopname", shopname);
        request.setAttribute("shopid", shopid);
        List<MallEtcSupplierEntity> mallEtcuppliers = etcSupplierService.loadAll(MallEtcSupplierEntity.class);
        request.setAttribute("mallEtcSuppliers", mallEtcuppliers);
        return new ModelAndView("net/lahuobao/mall/admin/mallEtcCardruleProductList");
    }


    /**
     * 添加商城ETC卡卡号管理
     * @param
     * @return
     */
    @RequestMapping(params = "save")
    @ResponseBody
    public AjaxJson save(MallEtcCardRuleEntity mallEtcCardRule, HttpServletRequest request) {
        String message = null;
        AjaxJson j = new AjaxJson();
        try {
            mallEtcCardRule.setCreateuserid(123456);
            mallEtcCardRuleService.save(mallEtcCardRule);
            message = "生成成功！";
        }catch (ConstraintViolationException e){
            e.printStackTrace();
            j.setSuccess(false);
            message = "卡号已存在！";
        }catch (Exception e){
            e.printStackTrace();
            j.setSuccess(false);
            message = "系统异常！";
        }
        j.setMsg(message);
        return j;
    }

}
